<?php

/***
 * Class GlobalFactory is a small static xml factory that contains utility methods for
 * generating frequently used xml:
 ***/
class GlobalFactory {

   // ! Executor Method

   /**
	* A helper method that tries to determine whether an array is actually
	* a database result iterator. If so, convert the object to an array that
	* we can transform to XML.
	*
	* @param Mixed $result The actual data that will generate the XML node set
	* @param Boolean $return_multi_on_single If the result set has only one record, return a 1-dimensional array
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Array
	*/
	static public function convertDatabaseResource($result, $return_multi_on_single = false)
	{
		if(is_object($result) && method_exists($result, 'nextRow'))
		{
			if($result->getNumRows() == 1 && false == $return_multi_on_single)
			{
				return $result->nextRow();
			}

			$temp = array();

			while($row = $result->nextRow())
			{
				$temp[] = $row;
			}

			$result = $temp;
		}

		return $result;
	}


   // ! Executor Method

   /**
	* An all-purpose utility that can turn most data into a set of XML nodes.
	*
	* @param String $name Name of the XML node set
	* @param Mixed $fields The actual data that will generate the XML node set
	* @param Array $attributs Any attributes you wish to pass along to the parent node set
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function generateGenericXml($name, $fields, $attributes = array())
	{
		if(is_object($fields) && method_exists($fields, 'nextRow'))
		{
			$temp = array();

			while($row = $fields->nextRow())
			{
				$temp[] = $row;
			}

			$fields = $temp;
		}

		$Generic = new XmlElement($name);

		$Generic->addAttribute('count', sizeof($fields));

		if($attributes)
		{
			foreach($attributes as $key => $val)
			{
				$Generic->addAttribute($key, $val);
			}
		}

		foreach($fields as $entry)
		{
			$Entry = new XmlElement('entry');

			foreach($entry as $key => $value)
			{
				$Entry->addNode(new XmlElement($key, $value));
			}

			$Generic->addNode($Entry);
		}

		return $Generic;
	}


   // ! Executor Method

   /**
	* Used to transfer locale definitions to the main XML document
	*
	* @param String $locale Name of the locale that is currently in use
	* @param Array $definitions List of assigned locale definitions
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function getLocaleXml($locale, $definitions)
	{
		$Locale = new XmlElement('locale');

		$Locale->addAttribute('count', sizeof($definitions));
		$Locale->addAttribute('locale', $locale);

		foreach($definitions as $title => $word)
		{
			$Locale->addNode(new XmlElement('item', $word, array('title' => $title)));
		}

		return $Locale;
	}


   // ! Executor Method

   /**
	* Used to generate a list of errors for display in XSL templates
	*
	* @param Array $errors A single-depth array containing errors
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function generateErrorXml($errors)
	{
		$Errors = new XmlElement('errors');

		$Errors->addAttribute('count', sizeof($errors));

		foreach($errors as $field => $error)
		{
			$Errors->addNode(new XmlElement('item', $error, array('field' => $field)));
		}

		return $Errors;
	}


   // ! Executor Method

   /**
	* Used to pass pagesplitting information to the XSL template system
	*
	* @param Integer $total Total number of records
	* @param Integer $perpage How many records to display per page
	* @param Integer $current The currently accessed page
	* @param Integer $page_count Total number of pages
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function generatePageXml($total, $perpage, $current, $page_count)
	{
		return new XmlElement('pagination-info', false, array('total-entries' => (int) $total, 'per-page' => $perpage, 'page-count' => $page_count, 'current-page' => $current));
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function generateFormFieldXml($form, $fields)
	{
		$Fields = new XmlElement('form-fields');

			$Fields->addAttribute('form', $form);

			foreach($fields as $name => $value)
			{
				$Fields->addNode(new XmlElement($name, $value));
			}

		return $Fields;
	}
}

?>